package com.jushu.storbox.view.dialog;


import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jushu.storbox.R;

public class CloudProgressDialog extends ProgressDialog{

	private TextView mTextView;
	private ProgressBar mProgressBar;
	private Context context;
	private View mParentView;
	
	public CloudProgressDialog(Context context) {
		super(context);
		this.context = context;
		initView();
		// TODO Auto-generated constructor stub
//		try {
//			if (null == progressDialog) {
////				progressDialog = new ProgressDialog(context);
//				progressDialog = new ProgressDialogWithText(context, R.style.dialog);
//				Window wd = progressDialog.getWindow();
//				WindowManager.LayoutParams lp = wd.getAttributes();
//				lp.alpha = 0.5f;
//				wd.setAttributes(lp);
//				mTextView = (TextView) progressDialog.findViewById(R.id.tv_progress);
//				mProgressBar = (ProgressBar) progressDialog.findViewById(R.id.pb_dialog);
////    		progressDialog.setCancelable(false);
//				progressDialog.setCanceledOnTouchOutside(true);
////				mTextView.setText(context.getString(R.string.loading));
////				progressDialog.setMessage(context.getString(R.string.loading));
//			}
//		} catch (Exception e) {
//
//			Log.i("liuxiang", e.getMessage() + "  oncreat fail");
//			e.printStackTrace();
//		}
	}
	
	public CloudProgressDialog(Context context, int theme){
		super(context, theme);
		this.context = context;
		initView();
	}
	
	private void initView() {
		getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		mParentView = LayoutInflater.from(context).inflate(R.layout.progress_dialog_with_text, null);
	
		mTextView = (TextView) mParentView.findViewById(R.id.tv_progress);
		mProgressBar = (ProgressBar) mParentView.findViewById(R.id.pb_dialog);
//	progressDialog.setCancelable(false);
		setCanceledOnTouchOutside(true);
		mTextView.setText(context.getString(R.string.loading));
		
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(mParentView);
//		Window wd = getWindow();
//		WindowManager.LayoutParams lp = wd.getAttributes();
//		lp.alpha = 0.5f;
//		wd.setAttributes(lp);
	}
	
	public void setSystemDialog() {
//		try {
//			if (null != progressDialog && !progressDialog.isShowing()) {
//				progressDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);//
//			}
//		} catch (Exception e) {
//			// TODO: handle exception
//			e.printStackTrace();
//		}
	}
	
	public void needShowDialog() {
		try {
			if(!isShowing())
			show();
		} catch (Exception e) {
			Log.i("liuxiang", e.getMessage() + "  can not show");
			e.printStackTrace();
		}
		
	}
	
	public void needDismissDialog() {
		try {
//			if (null != progressDialog && progressDialog.isShowing()) {
//				progressDialog.dismiss();
//			}
			if(isShowing())
			dismiss();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public void updateMessage(String message) {
		try {
//			progressDialog.setMessage(message);
			mTextView.setText(message);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public void setProgress(int progress){
		try {
			mProgressBar.setProgress(progress);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
//	
//	public boolean isShowing(){
////		if(progressDialog == null)return false;
//		return isShowing();
//	}
}
